package com.bootdo.wx.service.impl;

import com.bootdo.common.utils.PageHelperUtils;
import com.bootdo.wx.dao.GoodsSpikeDao;
import com.bootdo.wx.dao.MemberUserDao;
import com.bootdo.wx.domain.MemberUserDO;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

import com.bootdo.wx.dao.GoodsSpikeTimeDao;
import com.bootdo.wx.domain.GoodsSpikeTimeDO;
import com.bootdo.wx.service.GoodsSpikeTimeService;



@Service
public class GoodsSpikeTimeServiceImpl implements GoodsSpikeTimeService {
	@Autowired
	private GoodsSpikeTimeDao goodsSpikeTimeDao;
	@Autowired
	private GoodsSpikeDao goodsSpikeDao;
	@Autowired
	private MemberUserDao memberUserDao;
	
	@Override
	public GoodsSpikeTimeDO get(String id){
		return goodsSpikeTimeDao.get(id);
	}
	
	@Override
	public List<GoodsSpikeTimeDO> list(Map<String, Object> map){
		return goodsSpikeTimeDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return goodsSpikeTimeDao.count(map);
	}
	
	@Override
	public int save(GoodsSpikeTimeDO goodsSpikeTime){
		return goodsSpikeTimeDao.save(goodsSpikeTime);
	}
	
	@Override
	public int update(GoodsSpikeTimeDO goodsSpikeTime){
		return goodsSpikeTimeDao.update(goodsSpikeTime);
	}
	
	@Override
	public int remove(String id){
		return goodsSpikeTimeDao.remove(id);
	}
	
	@Override
	public int batchRemove(String[] ids){
		return goodsSpikeTimeDao.batchRemove(ids);
	}

	@Override
	public List<Map<String,Object>> getTime() {
		Map<String, Object> params = new HashMap<String,Object>();
		List<GoodsSpikeTimeDO> timeList = goodsSpikeTimeDao.getTime(params);
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		SimpleDateFormat shiFormat = new SimpleDateFormat("HH:mm");
		try {
			Date time = shiFormat.parse(shiFormat.format(new Date()).toString());
			for (GoodsSpikeTimeDO timeDO : timeList) {
				Map<String,Object> map = new HashMap<String,Object>();
				Date startAt = shiFormat.parse(timeDO.getStartAt().toString());
				Date endAt = shiFormat.parse(timeDO.getEndAt().toString());
				boolean panStart = startAt.before(time);  //开始时间小于现在时间
				boolean panEnd = endAt.before(time);  //结束时间小于现在时间
				if (!panStart) {
					map.put("ans","即将开始");
					map.put("can","1");
				} else if (panStart && !panEnd) {
					map.put("ans","正在秒杀");
					map.put("can","0");
				} else if (panEnd) {
					map.put("ans","已经结束");
					map.put("can","-1");
				}
				map.put("startTime",shiFormat.format(startAt));
				map.put("endTime",shiFormat.format(endAt));
				map.put("timeId",timeDO.getId());
				list.add(map);
			}
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return list;
	}

	/*获取秒杀商品列表*/
	@Override
	public PageInfo<Map<String,Object>> getGoodsByTime(Map<String, Object> paramMap) {
		String userId = com.bootdo.common.utils.StringUtils.getString(paramMap.get("userId"));
		MemberUserDO memberUserDO = memberUserDao.get(userId);
		int level = 0;
		if (memberUserDO != null) {
			paramMap.put("level", memberUserDO.getLevel());
			level = memberUserDO.getLevel();
		} else {
			paramMap.put("level", 0);
			level = 0;
		}
		PageHelperUtils.page(paramMap);
		List<Map<String,Object>> goodsList =goodsSpikeDao.getGoodsByTime(paramMap);
		for (Map<String, Object> goodsMap : goodsList) {
			int isvip = Integer.valueOf(goodsMap.get("isvip").toString());
			String imgurl = goodsMap.get("imgurl").toString();
			List<String> imgList = new ArrayList<String>();
			imgList.add(imgurl);
			goodsMap.remove("imgurl");
			goodsMap.put("imgList",imgList);
			Object price = goodsMap.get("price");
			Object price_c = goodsMap.get("price_c");
			Object price_z = goodsMap.get("price_z");
			Object price_g = goodsMap.get("price_g");
			if (isvip == 0) {
				goodsMap.put("minePrice",price);
			} else {
				switch (level) {
					case 1 :
						goodsMap.put("minePrice",price_c);
						break;
					case 2 :
						goodsMap.put("minePrice",price_z);
						break;
					case 3 :
						goodsMap.put("minePrice",price_g);
						break;
					default:
						goodsMap.put("minePrice",price);
				}
			}
		}
		PageInfo<Map<String,Object>> pageInfo = new PageInfo<>(goodsList);
		return pageInfo;
	}


	
}
